const http=require("http");
const fs=require("fs");
const url=require("url");
const qs=require("querystring")

const httpServer=http.createServer((req,res)=>{
    if(url.parse(req.url,true).pathname.includes("/api")){
        dataHandle(req,res);
    }
    else{staticData(req,res);}
})
httpServer.listen(3000,function(){console.log("http://localhost:3000 is ok")})
function staticData(req,res){

    fs.readFile("./dist"+url.parse(req.url,true).pathname,(err,data)=>{
        if(err){res.write("not found")}
        else{res.write(data)}
        res.end()
    })
}
function dataHandle(req,res){
    if(req.method==="GET"){
        const obj=url.parse(req.url,true).query;
        
        programHandle(req,res,obj);
    }
    else if(req.method==="POST"){
        
        let str=""
        req.on("data",(d)=>{str+=d})
        req.on("end",function(){const obj=qs.parse(str);programHandle(req,res,obj);})
    }
}
function programHandle(req,res,data){
    switch(data.type){
        case "getBanner":getbanner(res);break;
        case "getGoods":getgoods(res);break;
        case "getIndex":getIndex(res);break;/* 获取首页信息 */
        case "getLogin":getLogin(res);break;
        case "getProduct":getProduct(res);break;
        case "register":register(req, res, data);break;/*获取注册信息*/
        case "login":login(req, res, data);break;/*获取登录信息*/
        case "resetpassword":restpassword(req,res,data);break;
        default:console.log("error");

    }
}
function login(req, res, reqData){
    fs.readFile("./databases/users.json","utf-8",(err,data)=>{
        const userMsg = err ? [] : (data ? JSON.parse(data) : []);
        const obj = {};
        let i = 0;
        const flag = userMsg.some((val,idx)=>{
            i = idx;
            return val.username === reqData.username;
        })
        if(flag){
            if(userMsg[i].password === reqData.password){
                obj.code = 1;
                obj.title = "登录成功";
                obj.data = userMsg[i].username;
            }else{
                obj.code = 2;
                obj.title = "登录失败，密码不符";
                obj.data = "NOTDATA";
            }
        }else{
            obj.code = 0;
            obj.title = "登录失败，用户名不存在";
            obj.data = "NOTDATA";
        }
        res.write(JSON.stringify(obj));
        res.end();
    })
}

function register(req, res, reqData){
    fs.readFile("./databases/users.json","utf-8",(err,data)=>{
        const userMsg = err ? [] : (data ? JSON.parse(data) : []);
        const obj = {};
        if(userMsg.some(val=>val.username === reqData.username)){
            obj.code = 0;
            obj.title = "注册失败，用户名重复";
            obj.data = "NOTDATA";
            res.write(JSON.stringify(obj));
            res.end();
        }else{
            userMsg.push({
                username:reqData.username,
                password:reqData.password
            })
            fs.writeFile("./databases/users.json", JSON.stringify(userMsg),err=>{
                obj.code = 1;
                obj.title = "注册成功";
                obj.data = "NOTDATA";
                res.write(JSON.stringify(obj));
                res.end();
            })
        }
    })
}

function restpassword(req,res,reqData){
    fs.readFile("./databases/users.json","utf-8",(err,data)=>{
        const userMsg=JSON.parse(data);
        let i=0;
        let flag=false;
        for(let a=0;a<userMsg.length;a++){
            if(userMsg[a].username==reqData.username){
             i=a;
             if(userMsg[i].password==reqData.oldpassword){
                flag=true;
             }
             else{
                flag=false;
            }
            }
            else{
                flag=false;
            }
        }
        console.log(flag)
        if(flag){
            userMsg[i].password=reqData.newpassword;
            fs.writeFile("./databases/users.json", JSON.stringify(userMsg),err=>{
                res.write("修改成功");
                res.end();
            })
        }
        else{
            res.write("密码错误");
            res.end();
        }
    })
}



function getbanner(res){
    fs.readFile("./database/banner.json","utf-8",(err,data)=>{
        const obj={};
        if(err){
            obj.code=0;
            obj.title="banner数据查找失败"
            obj.data=[];
        }
        else{
            obj.code=1;
            obj.title="数据查找成功"
            obj.data=JSON.parse(data);
        }
        res.write(JSON.stringify(obj));
        res.end();
    })
}
function getgoods(res){
    fs.readFile("./www/database/goodsBasic.json","utf-8",(err,data)=>{
        const obj={};
        if(err){
            obj.code=0;
            obj.title="goodsBasic数据查找失败"
            obj.data=[];
        }
        else{
            obj.code=1;
            obj.title="数据查找成功"
            obj.data=JSON.parse(data);
        }
        res.write(JSON.stringify(obj));
        res.end();
    })
}
function getLogin(res){
    fs.readFile("./database")
}
function getIndex(res){}
function getProduct(res){}